package utils;

/**
 * 排序工具类
 *
 * @author yuh
 * @date 2018/5/14 20:27
 */
public class SortUtils {

    private SortUtils() {
    }

    /**
     * a比b大
     *
     * @param a
     * @param b
     * @return
     */
    public static boolean largerThan(Comparable a, Comparable b) {
        return a.compareTo(b) > 0;
    }

    /**
     * 在arr中交换i和j
     *
     * @param arr
     * @param i
     * @param j
     */
    public static void swap(Comparable[] arr, int i, int j) {
        Comparable tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    /**
     * 判断是否有序
     * @param arr
     */
    public static void isSorted(Comparable[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            if (largerThan(arr[i], arr[i + 1])) {
                throw new RuntimeException("this array has not been sorted");
            }
        }
    }

}
